package cn.web.demo.controller;

import cn.web.demo.model.CouresResult;
import cn.web.demo.model.CourseCategory;
import cn.web.demo.model.CourseOrder;
import cn.web.demo.model.PageResult;
import cn.web.demo.service.impl.CourseCategoryServiceImpl;
import cn.web.demo.service.impl.CouseOrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/courseorder")
public class CourseOrderController {


    @Autowired
    CouseOrderServiceImpl couseOrderService;

    @Autowired
    CourseCategoryServiceImpl courseCategoryService;

    @RequestMapping("/list")
    public String list(){
        return "/courseorder/list";
    }

    @RequestMapping("/listjson")
    @ResponseBody
    public PageResult<CourseOrder> orderjson(CourseOrder courseOrder,int page,int limit){
       // System.out.println(courseOrder.toString());
        PageResult<CourseOrder> pageResult=couseOrderService.findPageResult(courseOrder, page, limit);
        return pageResult;
    }

    @RequestMapping("/add")
    public String add(Model model){
        List<CourseCategory> list=courseCategoryService.findAllCourseCategory();
        model.addAttribute("categoryList", list);
        return "/courseorder/add";
    }

    @RequestMapping("save")
    @ResponseBody
    public CouresResult save(CourseOrder courseOrder){
        CouresResult couresResult=new CouresResult();
        if(courseOrder.getOrder_id() !=null && courseOrder.getOrder_id().length()!=0){
            couseOrderService.update(courseOrder);
        }else {
            couseOrderService.save(courseOrder);
        }
        return couresResult;
    }

    @RequestMapping("/detail")
    public String detail(Model model,String order_id){
        CourseOrder courseOrder= couseOrderService.findById(order_id);
        model.addAttribute("order",courseOrder);
        return "/courseorder/detail";
    }

    @RequestMapping("/edit")
    public String edit(Model model,String order_id){
        CourseOrder courseOrder= couseOrderService.findById(order_id);
        model.addAttribute("order",courseOrder);
        List<CourseCategory> list=courseCategoryService.findAllCourseCategory();
        model.addAttribute("categoryList", list);
        return "/courseorder/edit";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public CouresResult delete(String order_id){
        CouresResult couresResult=new CouresResult();
        couseOrderService.DelectById(order_id);
        return couresResult;
    }




}
